Skip to content

introduce @_manualOwnership performance attribute #81858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kavon
Copy link
Member

@kavon kavon commented May 30, 2025

This attribute forces programmers to acknowledge every copy that is required to happen in the body of the function. only those copies that make sense according to Swift's ownership rules are "required". For example,

func consumingFunc(_ t0: consuming Triangle) {}

@_manualOwnership
func basic_function_call(_ t1: Triangle) {
  consumingFunc(t1)  // error: explicit 'copy' required here
}

The way this is implemented is to flag every implicitly-created copy by SILGen in a function as an error. This keeps SILGen and the Swift language in sync about what copies are actually happening, which motivates fixes for cases where SILGen can be more clever.

This also means that there is no hand-holding to avoid exclusivity violations. Static checking will continue to catch some cases, but for other cases, like class objects, it is up to the programmer to understand where they may need to rewrite code with mutable references. This is where the "manual" comes from. For example,

// unsafe if x === y
swap(&x.a, &y.a)

// a safe rewrite
let t = copy x.a
x.a = copy y.a
y.a = consume t

This attribute forces programmers to acknowledge every
copy that is required to happen in the body of the
function. only those copies that make sense according
to Swift's ownership rules are "required".

The way this is implemented is to flag every non-explicit
copy in a function as an error. This keeps SILGen and the
Swift language in sync about what copies are actually
happening, which motivates fixes for cases where SILGen
can be more clever.

This also means that there is _no_ hand-holding to avoid
exclusivity violations. Static checking will continue to
catch some cases, but for other cases, like class objects,
it is up to the programmer to understand where they may
need to rewrite code with mutable references. This is
where the "manual" comes from. For example,

```swift
// unsafe if x === y
swap(&x.a, &y.a)

// a safe rewrite
let t = copy x.a
x.a = copy y.a
y.a = consume t
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant